package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.User;
import com.mapper.UserMapper;
import com.service.IUserService;
import com.util.DateUtil;
import com.util.MD5;
import com.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * InnoDB free: 4096 kB 服务实现类
 * </p>
 *
 * @author zzw
 * @since 2022-05-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    private Logger logger= LoggerFactory.getLogger( UserServiceImpl.class );
    @Override
    public Result login(String phone,String password) {
        password=MD5.md5(password);
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.like("phone", phone).like("password", password).in("power",10,50);
        List list= userMapper.selectList(wrapper);
        Result result=new Result();
        if(list.size()!=1){
            result.setCode(0);
            result.setMsg("查询失败");
            return result;
        }
        result.setCode(1);
        result.setData(list);
        return result;
    }

    @Override
    public Result register(String password, String phone) {
        password=MD5.md5(password);
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.like("phone", phone);
        List list= userMapper.selectList(wrapper);
        Result result=new Result();
        if ( list==null||list.size()==0 ){
            User user=new User();
            user.setPassword(password);
            user.setPhone(phone);
            user.setCreateAt(DateUtil.getNowDay());
            user.setPower(10);
            int flag=userMapper.insert(user);
            if ( flag==1 ){
                result.setCode(1);
                result.setMsg("注册成功");
            }else{
                result.setCode(0);
                result.setMsg("系统出现故障，请稍后再试或联系管理员");
            }
        }else{
            result.setCode(0);
            result.setMsg("手机号已被占用，请更换手机号");
        }
        return result;
    }
}
